package com.txzt.gaea.base.controller;

import com.txzt.base.constant.RtnConstant;
import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.base.dto.DictDetailQueryDTO;
import com.txzt.gaea.base.model.DictDetail;
import com.txzt.gaea.base.service.DictDetailService;
import com.txzt.gaea.base.vo.DictDetailVo;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.page.PageInfoBT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dict/detail")
public class DictDetailController extends BaseController {

    @Autowired
    private DictDetailService dictDetailService;

    @PostMapping(value = "/list")
    public RtnInfo<PageInfoBT<DictDetailVo>> pageList(@RequestBody DictDetailQueryDTO dictDetailQueryDTO){
        return dictDetailService.list(dictDetailQueryDTO);
    }

    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo add(@RequestBody DictDetail dictDetail){
        if (null != dictDetail && null == dictDetail.getId()){
            //查重
            if (!dictDetailService.addCheckValue(dictDetail)){
                return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"存在重复数据，请检查字典值");
            }
            //序号自动+1
//            dictInfo.setSort(dictInfoService.getLastSort()+1);
            int num = dictDetailService.save(dictDetail);
            if (0 == num) {
                return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"数据添加失败，请联系管理员");
            }else{
                return RtnInfo.success(num);
            }
        }else {
            return RtnInfo.PARAM_MISSING;
        }
    }

    @GetMapping(value = "/getLastSort/{dictId}")
    public RtnInfo getLastSort(@PathVariable Long dictId) {
        return RtnInfo.success(dictDetailService.getLastSort(dictId));
    }

    @GetMapping(value = "/item/{id}")
    public RtnInfo getInfoById(@PathVariable Long id){
        return dictDetailService.getDictDetail(id);
    }

    @PostMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo edit(@RequestBody DictDetail dictDetail){
        if (null != dictDetail && null != dictDetail.getId()){
            //查重
            if (!dictDetailService.updateCheckValue(dictDetail)){
                return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"存在重复数据，请检查字典值");
            }
            int num = dictDetailService.update(dictDetail);
            if (0 == num) {
                return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"数据更新失败，请联系管理员");
            }else{
                return RtnInfo.success(num);
            }
        }else {
            return RtnInfo.PARAM_MISSING;
        }
    }

    @GetMapping(value = "/delete/{id}")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo remove(@PathVariable Long id){
        return RtnInfo.success(dictDetailService.delete(id));
    }


}
